//
//  RelaxViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "RelaxViewController.h"
#import "MenuControlView.h"
#import "WWRelaxCollectionCell.h"
#import "WWGifCollectionCell.h"
#import "WWCartoonCollectionCell.h"

@interface RelaxViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, MenuControlViewDelegate>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)MenuControlView *menuView;

@end

static NSString *systemCell = @"systemCell";

@implementation RelaxViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.collectionView];
    self.title = @"轻松一刻";
}

#pragma mark - 懒加载
#pragma mark 初始化collectionView
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(GViewWidth(self.view), GViewHeight(self.view) - 64 - 49 - FitConstant_YorHeight(50));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, FitConstant_YorHeight(50), self.view.frame.size.width, GViewHeight(self.view) - 64 - 49 - FitConstant_YorHeight(50)) collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        [self.collectionView NightWithType:UIViewColorTypeNormal];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:systemCell];
        //        注册cell
        [_collectionView registerClass:[WWRelaxCollectionCell class] forCellWithReuseIdentifier:@"comicCell"];
        [_collectionView registerClass:[WWGifCollectionCell class] forCellWithReuseIdentifier:@"gifCell"];
        [_collectionView registerClass:[WWCartoonCollectionCell class] forCellWithReuseIdentifier:@"cartoonCell"];
    }
    return _collectionView;
}

#pragma mark 初始化menuView
-(MenuControlView *)menuView
{
    if (!_menuView) {
        _menuView = [[MenuControlView alloc] initWithFrame:CGRectMake(0, 0, FitConstant_XorWidth(375), FitConstant_YorHeight(50))];
        _menuView.delegate = self;
        _menuView.numberOfMenu = 3;
        _menuView.dataSource = @[@"Gif", @"纯文", @"动漫"];
        [self.menuView NightWithType:UIViewColorTypeNormal];
    }
    return _menuView;
}

#pragma mark - collectionView代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.menuView.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        WWRelaxCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"comicCell" forIndexPath:indexPath];
        return cell;
    }
    if (indexPath.row == 1) {
        WWGifCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"gifCell" forIndexPath:indexPath];
        return cell;
    }
    if (indexPath.row == 2) {
        WWCartoonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cartoonCell" forIndexPath:indexPath];
        return cell;
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:systemCell forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor grayColor];
    
    return cell;
}

#pragma mark - scrollView代理方法, 用来控制menuView的切换
#pragma mark 结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x + FitConstant_XorWidth(50)) / scrollView.frame.size.width;
    [_menuView selectCell:page];
}

#pragma mark - MenuViewDelegate
-(void)didSelectedMenu:(NSInteger)number
{
    self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width * number, 0);
}


@end
